<!-- 
    自定义操作模板-代练通
-->

<!-- 自定义操作模板 -->
<script type="text/html" id="templet-order-tool">
  <div class="layui-clear-space">
    <a class="layui-btn layui-btn-sm" lay-event="posting">发单</a>
  </div>
</script>

<!-- 发单确认模态框 -->
<div id="DaiLianTong_confirmModal" style="display: none; padding: 20px;">
    <div class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">订单金额</label>
            <div class="layui-input-block">
                <input type="text" name="DaiLianTong_amount" id="DaiLianTong_Amount" lay-verify="required|number"
                    placeholder="请输入订单金额" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">效率保证金</label>
            <div class="layui-input-block">
                <input type="text" name="DaiLianTong_efficiencyDeposit" id="DaiLianTong_EfficiencyDeposit"
                    lay-verify="required|number" placeholder="请输入效率保证金" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">游戏区服名称</label>
            <div class="layui-input-block">
                <input type="text" name="DaiLianTong_gameRegionName" id="DaiLianTong_GameRegionName"
                    placeholder="请输入游戏区服名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <!-- 隐藏字段，存储订单SerialNo -->
        <input type="hidden" name="DaiLianTong_serialNo" id="DaiLianTong_SerialNo" value="">

        <div class="layui-form-item">
            <label class="layui-form-label">时间要求(小时)</label>
            <div class="layui-input-block">
                <input type="text" name="DaiLianTong_hour" id="DaiLianTong_Hour" lay-verify="required|number"
                    placeholder="请输入时间要求" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">安全保证金</label>
            <div class="layui-input-block">
                <input type="text" name="DaiLianTong_securityDeposit" id="DaiLianTong_SecurityDeposit"
                    lay-verify="required|number" placeholder="请输入安全保证金" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">订单标题</label>
            <div class="layui-input-block">
                <input type="text" name="DaiLianTong_title" id="DaiLianTong_Title" lay-verify="required"
                    placeholder="请输入订单标题" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" onclick="DaiLianTong_ConfirmSubmit()">确认发单</button>
            </div>
        </div>
    </div>
</div>


<script>

    // 获取订单数据
    window.DaiLianTong_GetOrderData = function () {
        window.DaiLianTong_OrderDataForm = {
            amount: document.getElementById('DaiLianTong_Amount').value,
            efficiencyDeposit: document.getElementById('DaiLianTong_EfficiencyDeposit').value,
            gameRegionName: document.getElementById('DaiLianTong_GameRegionName').value,
            hour: document.getElementById('DaiLianTong_Hour').value,
            securityDeposit: document.getElementById('DaiLianTong_SecurityDeposit').value,
            title: document.getElementById('DaiLianTong_Title').value,
            dailiantong_order_id: document.getElementById('DaiLianTong_SerialNo').value,
        }
    }

    window.DaiLianTong_Function_ZiDingYiCaoZuoMoBan = function (table, $) {
        var DaiLianTong_ConfirmModal_Index = null; // 存储发单确认模态框索引
        // 添加表格事件监听
        table.on('tool(DaiLianTong_OrderTable)', function (obj) {
            var data = obj.data; // 获得当前行数据
            var layEvent = obj.event; // 获得 lay-event 对应的值

            if (layEvent === 'posting') {
                // 显示订单详情模态框
                // 打开模态框，并保存索引
                confirmModalIndex = layer.open({
                    type: 1,
                    title: '确认发单 - 订单ID: ' + data.SerialNo,
                    content: $('#DaiLianTong_confirmModal'),
                    area: ['500px', 'auto'],
                    success: function () {
                        document.getElementById('DaiLianTong_Amount').value = data.Price;
                        document.getElementById('DaiLianTong_EfficiencyDeposit').value = data.Ensure2;
                        document.getElementById('DaiLianTong_GameRegionName').value = data.Zone;
                        document.getElementById('DaiLianTong_Hour').value = data.TimeLimit;
                        document.getElementById('DaiLianTong_SecurityDeposit').value = data.Ensure1;
                        document.getElementById('DaiLianTong_Title').value = data.Title;
                        document.getElementById('DaiLianTong_SerialNo').value = data.SerialNo;
                        DaiLianTong_GetOrderData();
                    },
                    end: function () {
                        // 隐藏模态框
                        $('#DaiLianTong_confirmModal').hide();
                        // 重置索引
                        DaiLianTong_ConfirmModal_Index = null;
                    }
                });

            }
        });
    }

    window.DaiLianTong_ConfirmSubmit = async () => {
        DaiLianTong_GetOrderData()
        // 验证表单
        if (!window.DaiLianTong_OrderDataForm) {
            layer.msg('请先获取订单数据');
            return;
        }
        // 提交表单
        console.log(window.DaiLianTong_OrderDataForm);

        const response = await axios({
            url: "/api/dailianwanzi/publish_order/",
            params: {
                title: window.DaiLianTong_OrderDataForm.title,
                amount: window.DaiLianTong_OrderDataForm.amount,
                efficiencyDeposit: window.DaiLianTong_OrderDataForm.efficiencyDeposit,
                gameRegionName: window.DaiLianTong_OrderDataForm.gameRegionName,
                hour: window.DaiLianTong_OrderDataForm.hour,
                securityDeposit: window.DaiLianTong_OrderDataForm.securityDeposit,
                dailiantong_order_id: window.DaiLianTong_OrderDataForm.dailiantong_order_id,
            },
            BS_ShowLoading: true,
        });

        if (response.data.code !== 0) {
            layer.msg(response.data.message);
            return;
        }

        console.log(response.data.data);
        
        layer.msg('订单发布成功');
        

        // layer.close(confirmModalIndex);
        return false;
    }

</script>